package com.gaoyi.crm.repository;


import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.callback.Callback;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.DbMsg;
import com.gaoyi.crm.util.OKHttpUtil;
import com.gaoyi.crm.util.Utils;

import java.lang.reflect.Type;

public class UtilRepository {
    public void upload(String path, final OnResultListener listener, Type typeofT) {
        if (Utils.isOnline()) {
            uploadOnline(path, listener, typeofT);
        } else {
            upload(path, listener);
        }
    }

    private void uploadOnline(String path, final OnResultListener listener, Type typeofT) {
        OKHttpUtil.getInstance().uploadFileAsync(OKHttpUtil.NET_URL + "/api/utils/uploadfile", path, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                listener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
//                upload(path,listener);
                listener.onFails(e);
            }
        }, typeofT);
    }

    private void upload(String path, final OnResultListener listener) {
        ApiResultData<String> result = new ApiResultData<>();
        result.setStatus(ApiStatus.SUCCESS);
        result.setMessage(DbMsg.UPLOAD_SUCCESS);
        result.setData(path);
        listener.onSuccess(result);
    }
}
